<?php

namespace common\logics;

use common\models\dynamic\DynamicVideo;
use Yii;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;
use common\models\dynamic\DynamicLikeLog;
use common\models\dynamic\DynamicStats;

/**
 * This is the model class for table "{{%dynamic}}".
 *
 * @property int $id
 * @property string $user_id 用户ID
 * @property int $direction 动态类型：0主动动态，1被动动态
 * @property int $retweet_id 转发动态原始ID
 * @property int $status
 * @property string $created_at
 * @property string $updated_at
 * @property string $deleted_at
 */
class TslineArticleLogic extends \common\models\tsline\TslineArticle {

    public function getPublishTime() {
        if ($this->created_at) {
            return Yii::$app->formatter->asRelativeTime($this->created_at);
        }
        return '很久以前';
    }

    public function getPics() {
        $gallery = $this->gallery;
        if (!empty($gallery)) {
            //return ArrayHelper::getColumn($gallery, 'picture');
        }
        return [];
    }

    public function getTools() {
        $stats = [
            'likes' => 0,
            'retweets' => 0,
            'comments' => 0,
        ];
        if ($this->stats) {
            $stats['likes'] = $this->stats->likes;
            $stats['retweets'] = $this->stats->retweets;
            $stats['comments'] = $this->stats->comments;
        }
        return $stats;
    }

//    public function getVideo() {
//        $d = rand(0, 1000) % 8;
//        return !$d ? [
//            'cover' => 'http://domain/path/video.m3u8',
//            'width' => 670,
//            'height' => 370,
//            'url' => 'http://domain/path/video.m3u8',
//            'duration' => '01:12',
//        ] : null;
//    }

    public function getUrl() {
        return Url::to(['/dynamic/default/view', 'id' => $this->id], true);
    }

    public function getAuthor() {
        return '鲁能体育';
    }

    public function fields() {
        return [
            'id',
            'format',
            'title',
            'thumbs',
            'author' => function() {
                return '鲁能体育';
            },
            'bloger' => function() {
                $bloger = $this->bloger;
                $bloger->avatar = Yii::getAlias('@attachUrl') . $bloger->avatar;
                return $bloger;
            },
            'pics',
            'video',
            'content',
            'stats' => function() {
                $stats = $this->stats;
                return [
                    'comments' => $stats->comments,
                    'likes' => $stats->likes,
                    'views' => $stats->views
                ];
            },
            'reward' => function() {
                return false;
            },
            'tag' => function() {
                $boolean = rand(0, 1000) % 8;
                return !$boolean ? ['text' => '官方', 'type' => 'danger'] : null;
            },
            'liked',
            'time' => 'publishTime',
            'created_at'
        ];
    }

}
